﻿
using Microsoft.AspNetCore.SignalR;
using Newtonsoft.Json;
using SpringBean.netty.handler;
using SpringBean.netty.enums;
using SpringBean.netty.handler.impl;
using SpringBean.netty.model;

namespace SpringBean.netty;

public class ChatHub : Hub
{
    public static Dictionary<string, string> user = new();
    
    // Connect是连接的实例方法，当客户端成功后在回调使用此方法，HandMsg是我的信息处理方法
    public void Connect(string key)
    {
        if (user.ContainsKey(key)) { user.Remove(key); }
        user.Add(key,Context.ConnectionId);
        Console.WriteLine("("+ DateTime.Now +")客户端："+ key + "上线成功!");
    }
    

    public async Task HandMsg(Chat chat)
    {
        chat.time = DateTime.Now;
        ChatHandlerService handler;
        switch ( (Command) chat.cmd)
        {
            case Command.PRIVATE:
                handler = new PrivateServiceImpl();
                await handler.execute(chat,user,Context,Clients);
                break; 
            case Command.LINERADIO:
                handler = new LineradioServiceImpl();
                await handler.execute(chat, user, Context, Clients);
                break;
        }
    }
}